package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.dto.ForumPostDTO;
import org.csu.onlineedubackend.entity.ForumPost;
import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.service.ForumPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import java.util.List;

@RestController
@RequestMapping("/api/forum/post")
public class ForumPostController {
    @Autowired
    private ForumPostService forumPostService;

    /**
     * 發佈新帖子
     */
    @PostMapping("/create")
    public boolean createPost(@RequestBody ForumPost post) {
        return forumPostService.createPost(post);
    }

    /**
     * 編輯帖子
     */
    @PostMapping("/update")
    public boolean updatePost(@RequestBody ForumPost post) {
        return forumPostService.updatePost(post);
    }

    /**
     * 刪除帖子
     */
    @DeleteMapping("/delete/{postId}")
    public boolean deletePost(@PathVariable Integer postId, @RequestParam Integer userId) {
        return forumPostService.deletePost(postId, userId);
    }

    /**
     * 分頁查詢帖子列表
     */
    @GetMapping("/list")
    public IPage<ForumPost> getPostPage(@RequestParam int pageNum, @RequestParam int pageSize,
                                        @RequestParam(required = false) Integer tagId,
                                        @RequestParam(required = false) Integer userId,
                                        @RequestParam(required = false) String keyword) {
        Page<ForumPost> page = new Page<>(pageNum, pageSize);
        return forumPostService.getPostPage(page, tagId, userId, keyword);
    }

    /**
     * 查詢單個帖子詳情
     */
    @GetMapping("/detail/{postId}")
    public ForumPost getPostDetail(@PathVariable Integer postId) {
        return forumPostService.getPostDetail(postId);
    }

    /**
     * 根據用戶查詢帖子
     */
    @GetMapping("/user/{userId}")
    public List<ForumPost> getPostsByUser(@PathVariable Integer userId) {
        return forumPostService.getPostsByUser(userId);
    }

    /**
     * 獲取用戶的發帖總數
     */
    @GetMapping("/count/{userId}")
    public CommonResponse<Integer> getUserPostCount(@PathVariable Integer userId) {
        int count = forumPostService.getUserPostCount(userId);
        return CommonResponse.success(count);
    }
} 